import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
import TableControls from '#cpt/temp/TableControls'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Box 	= $Lazy.load(()=>import('#tp/box/box'))
const $cache = $Lazy.file(()=>import('#private/cache-api'))

const WeekData = [
	{ label:'周一', value:'周一' },
	{ label:'周二', value:'周二' },
	{ label:'周三', value:'周三' },
	{ label:'周四', value:'周四' },
	{ label:'周五', value:'周五' },
	{ label:'周六', value:'周六' },
	{ label:'周日', value:'周日' }
]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		formData : [
			{ label: '选择地区',  		name: 'areaData', 		half:true, required:true, type:'address', onRef: ref=> this.address = ref, onValue:({ rows }) => {
				this.submit = { ...this.submit, ...rows }
			}},
			{ label: '具体位置',  		name: 'street', 		half:true, required:true },
			{ label: '标本收取地点',  	name: 'contact_name', 	half:true, required:true },
			{ label: '医生',  			name: 'doctor_id', 		half:true, type:'select'},
			{ label: '医院科室',  		name: 'depart_id', 		half:true, type:'select'},
			{ label: '联系电话',			name: 'contact_phone',	half:true },
			{ label: '上门周期',			name: 'to_door_cycle',	full:true, type:'checkboxGroup', data: WeekData  },
			{ label: '每周上门时',		name: 'to_door_time',	half:true, required:true, type:'time' },
			{ label: '个性化要求',		name: 'require',		full:true, type:'textarea' },
			{ label: '备注',				name: 'remark',			full:true, type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		
	}
	
	load = hosp_id => {
		this.query = this.submit = { hosp_id }
		if(this.props.disabled){ this.setState({ selectedKeys: null }) }
		this.fetch()
	}
	
	// 获取弹窗下拉框数据
	getModalSelect = async () => {
		const { formData } = this.state
		
		$fn.setModalRows(formData, ['doctor_id', 'depart_id'], ()=>{
			$cache.then( async f => {
				// 医生
				$fn.setRow(formData, 'doctor_id', await f.getCache({ api: 'doctor'}))
				// 医院科室
				$fn.setRow(formData, 'depart_id',  await f.getCache({ api: 'hospital_ke_shi'}))
				//
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bs-box-address/index')
	
	// 表单
	cols = [
		{ title: '序号', 			dataIndex: 'id', 			width:60 },
		{ title: '标本收取地点', 	dataIndex: 'contact_name', 	width:150 },
		{ title: '上门周期', 		dataIndex: 'to_door_cycle', width:250, render: text => $fn.hasArray(text) ? text.join('、') : '' },
		{ title: '每日上门时间', 	dataIndex: 'to_door_time', 	width:150 },
		{ title: '个性化要求', 		dataIndex: 'require', 		width:150 },
		{ title: '备注', 			dataIndex: 'remark', 		width:150 },
		TableControls.controls.call(this, { width: 120 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'bs-box-address/info', {uuid}, {
				select: this.getModalSelect,
				disabled: this.props.disabled,
				address: value => this.address.load(value)
			}),
			this.onDel([uuid])
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this,{click:this.getModalSelect, code:false, disabled: this.props.disabled}),
			this.onDel()
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'bs-box-address/delete', { uuid, isKeys: !uuid, disabled: this.props.disabled })
	
	render(){
		return (
			<>
				<Box title='标本收取地点' small controls={this.controls()} className='mt5 w' contentClassName={$fn.hasArray(this.state.data) ? 'pt10' : 'ptb10'}>
					{/* 表格 */}
					{ Table.call(this, { wrapStyle: {height:350} }) }
				</Box>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ labelWidth:'s9', width:800, api: 'bs-box-address/add', editApi: 'bs-box-address/edit'}) }
			</>
		)
	}
}

export default Index